AC_PREREQ(2.61)
AC_INIT(google-authenticator, 1.11, habets@google.com)
AC_CONFIG_SRCDIR([src/google-authenticator.c])
AC_CONFIG_AUX_DIR([build])
AC_CONFIG_MACRO_DIR([build])

# --enable-silent-rules
m4_ifdef([AM_SILENT_RULES],
  [AM_SILENT_RULES([yes])],
  [AC_SUBST([AM_DEFAULT_VERBOSITY], [1])])

AC_USE_SYSTEM_EXTENSIONS
AM_INIT_AUTOMAKE([foreign subdir-objects])
AM_MAINTAINER_MODE([enable])

LT_INIT
AC_PROG_CC
AC_PROG_CC_STDC

AC_CHECK_HEADERS([ \
        sys/fsuid.h \
        selinux/selinux.h \
        selinux/context.h \
])
AC_CHECK_FUNCS([ \
	explicit_bzero \
	setfsuid \
	setfsgid \
])

AC_CHECK_HEADERS_ONCE([security/pam_appl.h])
# On Solaris at least, <security/pam_modules.h> requires <security/pam_appl.h>
# to be included first
AC_CHECK_HEADER([security/pam_modules.h], [], [], \
		[#ifdef HAVE_SECURITY_PAM_APPL_H
		# include <security/pam_appl.h>
		#endif
		])
AC_CHECK_LIB([pam], [pam_get_user], [:])
AS_IF([test "x$ac_cv_header_security_pam_modules_h" = "xno" \
       -o "x$ac_cv_lib_pam_pam_get_user" = "xno"], [
  AC_MSG_ERROR([Unable to find the PAM library or the PAM header files])
])

AC_MSG_CHECKING([whether certain PAM functions require const arguments])
AC_LANG_PUSH(C)
# Force test to bail if const isn't needed
AC_LANG_WERROR
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
	#include <security/pam_appl.h>
	#include <security/pam_modules.h>
]],[[
	const void **item = 0;
	int dummy = 0;
	/*
	 * since pam_handle_t is opaque on at least some platforms, give it
	 * a non-NULL dummy value
	 */
	 const pam_handle_t *ph = (const pam_handle_t *)&dummy;
	(void) pam_get_item(ph, 0, item);
]])],[AC_DEFINE([PAM_CONST], [const], \
	[Define if certain PAM functions require const arguments])
		AC_MSG_RESULT([yes])],
	[AC_DEFINE([PAM_CONST], [], \
	[Prevent certain PAM functions from using const arguments])
	AC_MSG_RESULT([no])])


	AC_MSG_CHECKING(whether compiler understands -Wall)
	old_CFLAGS="$CFLAGS"
	CFLAGS="$CFLAGS -Wall"
	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],
	  AC_MSG_RESULT(yes),
	  AC_MSG_RESULT(no)
	  CFLAGS="$old_CFLAGS")

AC_LANG_POP(C)

# SELinux support (optional)
AC_ARG_ENABLE([selinux],
    AS_HELP_STRING([--disable-selinux], [Disable SELinux support]),
    [enable_selinux=$enableval],
    [enable_selinux=auto])

SELINUX_LIBS=""
have_selinux=no

if test "x$enable_selinux" != "xno"; then
    PKG_CHECK_MODULES([SELINUX], [libselinux], [
        have_selinux=yes
        AC_DEFINE([HAVE_SELINUX], [1], [Define if SELinux is available])
    ], [
        if test "x$enable_selinux" = "xyes"; then
            AC_MSG_ERROR([SELinux support requested but libselinux not found])
        fi
        have_selinux=no
    ])
fi

AC_SUBST([SELINUX_CFLAGS])
AC_SUBST([SELINUX_LIBS])
AM_CONDITIONAL([HAVE_SELINUX], [test "x$have_selinux" = "xyes"])


AC_SEARCH_LIBS([dlopen], [dl])

AC_CONFIG_HEADER(config.h)
AC_CONFIG_FILES([Makefile contrib/rpm.spec])
AC_OUTPUT

echo "
  $PACKAGE_NAME version $PACKAGE_VERSION
  Prefix.........: $prefix
  Debug Build....: $debug
  C Compiler.....: $CC $CFLAGS $CPPFLAGS
  Linker.........: $LD $LDFLAGS $LIBS
  SELinux CFlags.: $SELINUX_CFLAGS
  SELinux libs...: $SELINUX_LIBS
"
